2. Python Basics

  1. Expressions, and the online interactive shell, called Trinket
    • The right side pane is the interactive shell to execute Python expressions.
    • The left side pane is used for the code in a Python file.
    • Let's try the followings in the interactive shell.
      • 34 - 9
      • 34 - 9 * 8
      • (34 - 9) * 8
    • Let's try the above in the program file, main.py, and run it using the "Run code interactively" button.
  2. The offline interactive shell, called IDLE, on Windows
    • Sometimes it is convenient to use Trinket, especially because you do not have to install any offline programs. However Trinket might work slowly when you use a bit long code or when the Trinket server is loaded.
    • IDLE - Python's Integrated Development and Learning Environment; You can dowload Python 3 from http://python.org/ and install in in your computer, or you can use the computers in the computer labs at TRU. Phyton 3 is already installed in the computer labs.
    • Run IDLE, and try the next examples one by one in the Shell.
      • 34 - 9
      • 34 - 9 * 8
      • (34 - 9) * 8
      • Shell -
      • Editor -
    • Let's download first.py, open it with IDLE, and run it by selecting "Run -> Rub Module". Note that all the Python files has the extension '.py'.
    • Let's download bgp_lib.py and bgp_npuzzle.py, and try to run 'bgp_npuzzle.py'. You can enjoy an n-puzzle game.
  3. Arithmetic operators
    • There are many operators in Python, and some of them are used for arithmetic computations like Mathematics.
      • +    Addition
      • -    Subtraction
      • *    Multiplication
      • /    Division (oac floating-point division)
      • **    Exponentiation
      • %    Modulus (remainder)
      • //    Quotient (oac integer division)
    • Let's try 8 // 4 ** 3 + 14 / 7 - 5 % 2 in the interactive shell. What is the result? The question is which operators will be used first.
    • Precedence rules: **;  %, //, /, *;  -, +
    • From left to the right among the operators of the same precedence
    • (...) can be used to change the precedence. Let's try (8 // 4) ** 3 + 14 / (7 - 5) % 2.
  4. Syntax errors
    • When the syntax (grammar) is wrong, the interactive shell shows what is wrong. (Note that programming languates use almost identical arithmetic operations that we learned in elementary schools and high schools.)
    • E.g., 34 - * 9 8
    • Let's try the above expression in the next interactive shell. How can we fix?
  5. Data types - integers, floating point values (floats), strings
    • Integers: 2, 3, -45, ...
    • Floating point values (very similar to real numbers in Mathematics): 2.0, -3.88, 0.0, 1 / 3
    • Strings: 'a', "a", 'abc', "abc", 'What a fun!', "What a fun!"
    • Let's try the above expressions.
  6. String operations
    • How to concatenate strings?
      + is used to concatenate two strings.
      E.g., '1. ' + "What a wonderful world!"
      E.g., 2.0 + "Programming is a lot of fun!"
      Let's try the above examples in the above interactive shell.
    • How to repeat a string?
      * is used to repeat multiple times.
      E.g., "What a wonderful world!" * 3
      E.g., 3 * "Programming is a lot of fun!"
      E.g., "What a wonderful world!" * 3.0
      E.g., "Programming is a lot of fun!" * "3"
      Let's try the above examples in the above interactive shell.

  7. How to store values?
    • A variable is a computer's memory location where a value can be stored temporally, not permanently.
    • E.g., name = "John"    # name is a variable. It is assigned with "John".
    • E.g., age = 21    # age is a variable. It is assigned with 20, or we say 20 is assigned to age.
    • Let's try the above examples.
    • Naming restrictions
      • One word, no space
      • Only letters, digits, and '_'
      • Cannot start with a digit
    • Let's try _name, name0, 1name, name&age intead of name. Any syntax errors?

  8. How to print variables and values in a program?
    • The function print() is used to print strings.
    • E.g., print(name)    # after name = "John"
    • E.g., print(age)    # after age = 21
    • Let's try print(name + " " + age). Any error message?
    • How to convert a string to the corresponding integer or float?
      • The functions int() and float() are used to convert strings to integers and floats respectively. E.g., age = int("21"); int("21.7"); int(21.7);
    • How to convert an integer to the corresponding string?
      • The function str() is used to convert integers and floats to strings. E.g., age = 21; age_str = str(age); print(age_str)
    • How to get the lenght of a string?
      • The function len() is used to get the length of a string. E.g., print(len(name))

  9. How to read values from the user?
    • The function input() is used to read a string from the user.
    • Let's try name = input("Name: "); age = input(); print(name + ", " + age).
    • Let's try print("Age is " + 21). What is wrong? How to fix it?

  10. Programming exercises
    1. Write a program that prints the following triangle.
          *
         * *
        *   *
       *     *
      *********
      
    2. Write a program that
      • reads your age and name, and
      • prints them.
      • The output should be like Name: John, Age: 21
    3. Write a program that
      • reads a mount of money in cents, and
      • print it in dollars, quaters, dimes, nickels, and cents.
      • E.g., 2367 -> 23 dollars, 2 quaters, 1 dime, 1 nickel, and 2 cents
      • Hints. You can use // and %. E.g.,
        cents = ???(???("Enter cents: "))
        dollars = cents // 100
        cents = cents - dollars * 100    # or cents = cents ??? 100
        print(dollars)
        print(cents)
        
    4. Tic-tac-toe game
      • Write a program that prints
         |O| 
        -+-+-
        X| |X  
        -+-+-
         | |    3 rows, and 3 columns; the location (oac position) of the first 'O' is (0, 1).
        
      • Include in the above program the code that reads row number and column number from the user. (Note that the tic-tac-toe board is like a 2 dimensional space having rows and columns.)
    5. 3×3 puzzle game
      • Write a program that prints
        +-+-+-+
        |1|2|3|
        +-+-+-+
        |4|5|6|
        +-+-+-+
        |7|8| |
        +-+-+-+  3 rows, and 3 columns; the location (oac position) of the first '6' is (1, 2).
        
      • Include in the above program the code that reads row number and column number from the user. (Note that the 3×3 board is like a 2 dimensional space having rows and columns.)
  11. References